//
//  MicroAddAppCell.swift
//  TinyUI
//
//  Created by 徐涛 on 2020/3/26.
//  Copyright © 2020 葛晓良. All rights reserved.
//

import UIKit
import TinyUI
//154+footLineHeight


typealias addBlock = (String) ->()

class MicroAddAppView: UICollectionReusableView {
    let bgview=UIButton()
    let imgTitle:UIImageView = UIImageView.init()
    let lblTitle:UILabel = UILabel.init()
    let imageSize:CGSize=CGSize(width: 16, height: 16)
    let bgviewHeight:CGFloat = 53;
    let footLineView:UIView=UIView.init()
    let footLineHeight:CGFloat=0
    var addB:addBlock?
    let dashLayer:CAShapeLayer = CAShapeLayer();
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layoutUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    required init!(element: Element!) {
        fatalError("init(element:) has not been implemented")
    }
    
    
    func layoutUI()  {//img 16 4 text 16 -->36 头 底 （bgviewHeight-36）/2
        let beside:CGFloat = 4;
        let inside:CGFloat = (bgviewHeight-16-4-16)/2.0;
        bgview.frame=CGRect(x: 25, y: beside, width: self.bounds.width-50, height: bgviewHeight)
        bgview.backgroundColor=UIColor.clear
        bgview.addTarget(self, action: #selector(touchDown), for: .touchUpInside)
        self.addSubview(bgview)
        self.dash()
        
        imgTitle.frame=CGRect(x:bgview.bounds.width/2.0-imageSize.width/2.0, y: inside, width: imageSize.width,height: imageSize.height)
        //        imgTixtle.center=CGPoint(x: self.bounds.width/2.0, y: self.bounds.height/2.0-16-imageSize.height/2.0)
        //        imgTitle.backgroundColor=UIColor.tyColor(withHex: "#cccccc")
        bgview.addSubview(imgTitle)
        lblTitle.frame=CGRect(x: 0, y: imgTitle.frame.origin.y+imgTitle.frame.size.height+4, width: bgview.bounds.width, height: 16)
        lblTitle.textAlignment=NSTextAlignment.center
        lblTitle.textColor=UIColor.gray
        lblTitle.text="添加"
        lblTitle.font=UIFont.systemFont(ofSize: 13)
        bgview.addSubview(lblTitle)
        //        footLineView.frame=CGRect(x: 0, y: self.bounds.height-footLineHeight, width: self.bounds.width, height: footLineHeight)
        //        footLineView.backgroundColor=UIColor.tyColor(withHex: "#33d8d8d8")
        //        self.addSubview(footLineView)
        
    }
    
    
    private func dash()  {
        //        let layer:CAShapeLayer=CAShapeLayer();
        dashLayer.frame=CGRect(x: 0, y: 0, width: bgview.bounds.width, height: bgview.bounds.height)
        dashLayer.backgroundColor=UIColor.clear.cgColor
        
        let path:UIBezierPath = UIBezierPath.init(roundedRect: dashLayer.frame, cornerRadius: 0)
        dashLayer.path=path.cgPath
        dashLayer.lineWidth = 1.0
        dashLayer.lineDashPattern=[NSNumber(value: 3), NSNumber(value: 3)]
        dashLayer.fillColor   = UIColor.clear.cgColor;
        dashLayer.strokeColor = UIColor.tyColor(withHex:"#cccccc").cgColor;
        bgview.layer.addSublayer(dashLayer)
    }
    
    
    
    //MARK:-config
    func config(param:Dictionary<String,String> ,state:TCMicroAppStoreState,dependence:NSDictionary,dependencePath:String) {
        let  isEn:Bool = (TinyLang.defaultLange()?.languages=="en")
        if let tColor:String = dependence["button-text-color"] as? String{
            lblTitle.textColor=UIColor.tyColor(withHex: tColor)
        }
        if let bColor:String = dependence["button-border-color"] as? String{
            dashLayer.strokeColor = UIColor.tyColor(withHex: bColor)?.cgColor
        }
        if state == TCMicroAppStoreState.edit {
            
            if let icon:String = dependence["button-done-icon"] as? String {
                MicroDataStoreHelper.loadImageWithUrlPath(path: icon, dependencePath: dependencePath){[weak self] (image) in
                    self?.imgTitle.contentMode=UIView.ContentMode.scaleAspectFit
                    self?.imgTitle.image=image
                }
            }else{
                self.imgTitle.contentMode=UIView.ContentMode.scaleAspectFill
                self.imgTitle.image=UIImage(named:"MicroAppStore.bundle/done")
            }
            if isEn {
                self.lblTitle.text="Done"
            }else{
                self.lblTitle.text="完成"
            }
        }else{
            if let icon:String = dependence["button-edit-icon"] as? String {
                MicroDataStoreHelper.loadImageWithUrlPath(path: icon, dependencePath: dependencePath){[weak self] (image) in
                    self?.imgTitle.contentMode=UIView.ContentMode.scaleAspectFit
                    self?.imgTitle.image=image
                }
            }else{
                self.imgTitle.contentMode=UIView.ContentMode.scaleAspectFill
                self.imgTitle.image=UIImage(named:"MicroAppStore.bundle/add")
            }
            if isEn {
                self.lblTitle.text="Add"
            }else{
                self.lblTitle.text="添加"
            }
        }
    }
    
    @objc func touchDown(){
        if  addB != nil {
            addB!("")
        }
    }
}





